Imports System.Data
Imports System.Data.SqlClient
Imports Conexao_liion

Partial Class FuncionarioFuncional
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

        Response.Cache.SetCacheability(HttpCacheability.ServerAndNoCache)

        If Not Page.IsPostBack Then

            CarregaDados()
            CarregaGVW()

            btnAlterar.Enabled = False
            btnExcluir.Enabled = False

        End If

    End Sub

    Private Sub CarregaGVW()

        Using theSqlConnection As New SqlConnection(Conexao_liion.theConnectionString)

            Dim mydatatable As New DataTable
            'Dim theQueryString As String = "SELECT LEFT(MesAnoDeCompetencia,2) + '.' + RIGHT(MesAnoDeCompetencia,4) AS MesAnoDeCompetencia," & _
            '         " NomeDaDiretoria, NomeDaDivisao, NomeDoDepartamento, CodigoDoCentroDeCusto" & _
            '         " FROM FuncionarioFuncional" & _
            '         " WHERE CodigoDaHolding = '" & Session("strCodDaHolding").ToString & "'" & _
            '         " AND CodigoDaCia = '" & Session("strCodDaCia").ToString & "'" & _
            '         " AND CodigoDoEstabelecimento = '" & Session("strCodDoEstabelecimento").ToString & "'" & _
            '         " AND NumeroDoRegistroDoEmpregado = '" & Session("strNumeroRegistroEmpregado").ToString & "'" & _
            '         " ORDER BY MesAnoDeCompetencia;"

            Dim theQueryString As String = "SELECT LEFT(A.MesAnoDeCompetencia,2) + '.' + RIGHT(A.MesAnoDeCompetencia,4) AS MesAnoDeCompetencia, B.NomeDaDiretoria, C.NomeDaDivisao, D.NomeDoDepartamento, CodigoDoCentroDeCusto " & _
                    "FROM FuncionarioFuncional A, diretoria B, divisao C, departamento D " & _
                    "WHERE A.CodigoDaHolding = B.CodigoDaHolding AND " & _
                    "A.CodigoDaCia = B.CodigoDaCia AND " & _
                    "A.CodigoDaDiretoria = B.CodigoDaDiretoria AND " & _
                    "A.CodigoDaHolding = C.CodigoDaHolding AND " & _
                    "A.CodigoDaCia = C.CodigoDaCia AND " & _
                    "A.CodigoDaDiretoria = C.CodigoDaDiretoria AND " & _
                    "A.CodigoDaDivisao = C.CodigoDaDivisao AND " & _
                    "A.CodigoDaHolding = D.CodigoDaHolding AND " & _
                    "A.CodigoDaCia = D.CodigoDaCia AND " & _
                    "A.CodigoDaDiretoria = D.CodigoDaDiretoria AND " & _
                    "A.CodigoDaDivisao = D.CodigoDaDivisao AND " & _
                    "A.CodigoDoDepartamento = D.CodigoDoDepartamento AND " & _
                    "A.CodigoDaHolding = '" & Session("strCodDaHolding").ToString & "' AND " & _
                    "A.CodigoDaCia = '" & Session("strCodDaCia").ToString & "' AND " & _
                    "A.CodigoDoEstabelecimento = '" & Session("strCodDoEstabelecimento").ToString & "' AND " & _
                    "A.NumeroDoRegistroDoEmpregado = '" & Session("strNumeroRegistroEmpregado").ToString & "' " & _
                    "ORDER BY A.MesAnoDeCompetencia;"


            Dim theSqlCommand As SqlCommand = theSqlConnection.CreateCommand()
            theSqlCommand.CommandText = theQueryString

            Try
                theSqlConnection.Open()
                Dim theSqlDataReader As SqlDataReader = theSqlCommand.ExecuteReader()

                mydatatable.Load(theSqlDataReader)

                gvwData.DataSource = mydatatable
                gvwData.DataBind()

                theSqlDataReader.Close()
            Catch ex As Exception
                'theSqlDataReader.Close()
                lblMsg.Text = "Msg DB: " & ex.Message
            End Try

        End Using

    End Sub

    Private Sub CarregaDados()

        Try
            lblHolding.Text = Session("strNomeDaHolding").ToString
            lblCia.Text = Session("strNomeDaCia").ToString
            lblEstabelecimento.Text = Session("strRazaoSocialDoEstabelecimento").ToString
            lblFilial.Text = "(Filial " & Session("strFilialDoEstabelecimento").ToString & ")"
            lblNome.Text = Session("strNumeroRegistroEmpregado").ToString & " - " & Session("strNomeEmpregado").ToString
        Catch ex As Exception
            lblMsg.Text = ex.Message
        End Try

    End Sub

    Protected Sub gvwData_PageIndexChanging(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewPageEventArgs) Handles gvwData.PageIndexChanging

        gvwData.PageIndex = e.NewPageIndex
        gvwData.DataBind()

    End Sub

    Protected Sub gvwData_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles gvwData.SelectedIndexChanged

        'If Not Page.IsPostBack Then
        btnAlterar.Enabled = True
        btnExcluir.Enabled = True
        'End If

    End Sub



    Protected Sub btnIncluir_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnIncluir.Click

        Session.Add("strOperacao", "I") ' Incluir

        Session.Add("strCodigoDoCentroDeCusto", "")

        Response.Redirect("Funcionario3Data.aspx")

    End Sub

    Protected Sub btnAlterar_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnAlterar.Click

        Session.Add("strOperacao", "A") ' Alterar

        Session.Add("strCodigoDoCentroDeCusto", HttpUtility.HtmlDecode(gvwData.SelectedRow.Cells(4).Text))

        Response.Redirect("Funcionario3Data.aspx")

    End Sub

    Protected Sub btnExcluir_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnExcluir.Click

        Session.Add("strOperacao", "E") ' Excluir

        Session.Add("strCodigoDoCentroDeCusto", HttpUtility.HtmlDecode(gvwData.SelectedRow.Cells(4).Text))

        Response.Redirect("Funcionario3Data.aspx")

    End Sub

    Protected Sub btnVoltar_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnVoltar.Click

        Response.Redirect("FuncionarioDados.aspx")

    End Sub

End Class
